package com.dzjcloud.dzj.upms.biz.controller.sys;


import cn.hutool.http.HttpRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzjcloud.dzj.common.core.constant.CommonConstants;
import com.dzjcloud.dzj.common.core.utils.R;
import com.dzjcloud.dzj.common.redis.utils.RedisUtil;
import com.dzjcloud.dzj.upms.api.entity.Gateway;
import com.dzjcloud.dzj.upms.biz.fegin.gatewayFeginService;
import com.dzjcloud.dzj.upms.biz.service.GatewayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 基础平台网关路由规则表(Gateway)表控制层
 *
 * @author makejava
 * @since 2023-04-10 09:25:40
 */
@RestController
@RequestMapping(CommonConstants.SYS+"/gateway")
@Api(value = "gateway", tags = "网关前端控制器")
@Slf4j
@RequiredArgsConstructor
public class GatewayController extends ApiController {
    /**
     * 服务对象
     */
    private final GatewayService gatewayService;

    /**
     * 网关服务对象
     */
    @Autowired
    private gatewayFeginService gatewayFeginService;


    /**
     * 新增数据
     *
     * @param gateway 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增网关")
    public R<Boolean> insert(@RequestBody Gateway gateway) {
        return R.ok(this.gatewayService.save(gateway));
    }

    /**
     * 分页查询所有数据
     *
     * @param page    分页对象
     * @param gateway 查询实体
     * @return 所有数据
     */
    @GetMapping
    @ApiOperation("分页查询所有数据")
    public R<Page<Gateway>> selectAll(Page<Gateway> page, Gateway gateway) {
        return R.ok(this.gatewayService.page(page, new QueryWrapper<>(gateway)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @ApiOperation("通过主键查询单条数据")
    public R<Gateway> selectOne(@PathVariable String id) {
        return R.ok(this.gatewayService.getById(id));
    }



    /**
     * 修改数据
     *
     * @param gateway 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation("修改数据")
    public R<Boolean> update(@RequestBody Gateway gateway) {
        return R.ok(this.gatewayService.updateById(gateway));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("删除数据")
    public R<Boolean> delete(@RequestParam("id") List<Long> idList) {
        return R.ok(this.gatewayService.removeByIds(idList));
    }

    @GetMapping("/refresh")
    @ApiOperation("网关刷新")
    public R<String> refresh(){
        //加载网关到redis
        gatewayService.reloadRouter();
        return gatewayFeginService.refresh();
    }
}

